package com.zmn.mcc.admin.controller.module;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.module.McModule;
import com.zmn.mcc.model.entity.module.McModuleQuery;
import com.zmn.mcc.model.entity.sys.McSys;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:46
 */
@Controller
@RequestMapping("/module/")
public class ModuleListController extends ModuleBaseController {


    /**
     * 模块列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @RequestBody McModuleQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setModuleId(Integer.parseInt(keyword));
            query.setModuleName("");
        } else {
            query.setModuleName(keyword);
        }

        List<McModule> items = moduleService.listPageByQuery(query);

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("syses", sysService.listAll());
        model.addAttribute("keyword", keyword);

        return "module/list";
    }

    /**
     * 模块树状列表
     */
    @RequestMapping("tree")
    public String config(HttpServletRequest request,
                         @ModelAttribute McModuleQuery query,
                         Model model) {

        McSys sys = NumberUtil.isNotNullOrZero(query.getSysId()) ? sysService.findByKey(query.getSysId()) : new McSys();

        model.addAttribute("sys", sys);
        model.addAttribute("vo", query);

        return "module/list_tree";
    }
    @RequestMapping("ajaxTree")
    @ResponseBody
    public ResponseDTO ajaxTree(HttpServletRequest request, HttpServletResponse response,
                         @ModelAttribute McModuleQuery query) {

        List<McModule> items;
        if(NumberUtil.isNullOrZero(query.getSysId())) {
            items = moduleBService.listModule(null, query.getStatus());
        } else {
            items = moduleBService.listModule(query.getSysId(), null, query.getStatus());
        }

        if(CollectionUtil.isNotNullOrEmpty(items)) {
            return ResponseDTO.success(items, "OK");
        } else {
            return ResponseDTO.fail("空的模块列表");
        }
    }

    /**
     * 模块详情
     */
    @RequestMapping("detail")
    public String detail(HttpServletRequest request,
                         @RequestParam(value = "sysId", defaultValue = "0") Integer sysId,
                         @RequestParam(value = "moduleId", defaultValue = "0") Integer moduleId,
                         @RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
                         Model model) {

        McModule obj = NumberUtil.isNotNullOrZero(moduleId) ? moduleService.findByKey(moduleId) : new McModule();
        McModule parent = new McModule();
        McSys sys = new McSys();
        if(NumberUtil.isNullOrZero(moduleId) && NumberUtil.isNotNullOrZero(parentId)) {
            obj.setParentId(parentId);
        }
        if(obj.getParentId() != null && obj.getParentId() > GlobalConsts.TOP_ID) {
            parent = moduleService.findByKey(obj.getParentId());
            obj.setSysId(parent.getSysId());
        }
        if(NumberUtil.isNullOrZero(obj.getSysId()) && NumberUtil.isNotNullOrZero(sysId)) {
            obj.setSysId(sysId);
        }
        if(NumberUtil.isNotNullOrZero(obj.getSysId())) {
            sys = sysService.findByKey(obj.getSysId());
        }

        model.addAttribute("obj", obj);
        model.addAttribute("parent", parent);
        model.addAttribute("sys", sys);

        return "module/detail";
    }
}
